package com.tanhua.server.service;

import com.alibaba.fastjson.JSON;
import com.tanhua.commons.templates.HuanXinTemplate;
import com.tanhua.domain.db.Question;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.vo.ContactVo;
import com.tanhua.domain.vo.MessageVo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.CommentApi;
import com.tanhua.dubbo.api.FriendApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Classname IMService
 * @Description TODO 回复陌生人信息
 * @Date 2021/3/16 15:19
 * @Created by gan
 */
@Service
public class IMService {
    @Autowired
    private HuanXinTemplate huanXinTemplate;

    @Reference
    private QuestionApi questionApi;

    @Reference
    private UserInfoApi userInfoApi;

    @Reference
    private FriendApi friendApi;

    @Reference
    private CommentApi commentApi;
    //回复陌生人信息
    public void replyStrangerQuestions(Map<String, Object> paraMap) {
        long userId = Long.parseLong(paraMap.get("userId").toString());
        String content =((String) paraMap.get("reply"));
        //获取问题
        Question question = questionApi.findByUserId(userId);
        //获取登录用户的信息
        UserInfo userInfo = userInfoApi.findByUserId(UserHolder.getUserId());
        //构建消息内容
        Map<String,String> map=new HashMap<String,String>();
        map.put("userId",userInfo.getId().toString());
        map.put("nickname",userInfo.getNickname());
        map.put("strangerQuestion",question==null?"你喜欢我吗？":question.getTxt());
        map.put("reply",content);
        String msg= JSON.toJSONString(map);
        //发信息给im
        huanXinTemplate.sendMsg(userId+"",msg);

    }


    //添加好友
    public void addContacts(Long friendId) {
        //添加好友
        Long userId=UserHolder.getUserId();
        friendApi.add(userId,friendId);
        //环信也加为好友
        huanXinTemplate.makeFriends(userId,friendId);
    }


    //联系人分页
    public PageResult<ContactVo> queryContactsList(Integer page, Integer size, String keyword) {
        //调用api获取用户的好友列表 PageResult<Friend>
        PageResult result=friendApi.findPage(UserHolder.getUserId(),page,size,keyword);
        //获取列表数据
        List<Friend> items = result.getItems();
        //循环遍历，构造vo
        List<ContactVo> list=new ArrayList<>();
        for (Friend item : items) {
            UserInfo userInfo=userInfoApi.findByUserId(item.getFriendId());
            //创建vo
            ContactVo vo=new ContactVo();
            BeanUtils.copyProperties(userInfo,vo);
            vo.setCity(StringUtils.substringBefore(userInfo.getCity(),"-"));
            vo.setUserId(userInfo.getId().toString());
            list.add(vo);
        }
        //返回
        result.setItems(list);
        return result;
    }



    //查询点赞，评论，喜欢列表
    public ResponseEntity<PageResult> messageCommentList(Integer type, Integer page, Integer pagesize) {
        //调用api获取各种数据列表
        PageResult result=commentApi.findByUserId(page,pagesize,type,UserHolder.getUserId());
        //获取数据,都是一些点赞，评论
        List<Comment> items = result.getItems();
        //循环遍历，创建vo集合
        List<MessageVo> messageVoList=new ArrayList<>();
        for (Comment record : items) {
            //创建vo，等下进行复制
            MessageVo messageVo=new MessageVo();
            //获取userInfo对象
            messageVo.setId(record.getId().toHexString());
            UserInfo userInfo = userInfoApi.findByUserId(record.getUserId());
            messageVo.setAvatar(userInfo.getAvatar());
            messageVo.setNickname(userInfo.getNickname());
            String createDate = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date(record.getCreated()));
            messageVo.setCreateDate(createDate);
            messageVoList.add(messageVo);
        }
        //构造返回
        result.setItems(messageVoList);
        return ResponseEntity.ok(result);
    }
}
